import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { MessageBox } from 'element-ui'
// 导入组件
import UserLogin from '@/views/login/UserLogin.vue'
import UserReg from '@/views/login/UserReg.vue'
import LayoutContain from '@/views/user/layout/LayoutContain.vue'
import HomePage from '../views/user/layout/HomePage.vue'
import MaintainerList from '@/views/user/maintainer/MaintainerList.vue'
import ServiceList from '@/views/user/maintainService/ServiceList.vue'
import UserCenter from '@/views/user/userCenter/UserCenter.vue'
import UserInfo from '@/views/user/userCenter/UserInfo.vue'
import ChangePassword from '@/views/user/userCenter/ChangePassword.vue'
import UserAddress from '@/views/user/userCenter/UserAddress.vue'
import UserOrder from '@/views/user/order/UserOrder.vue'
import ServiceDetail from '@/views/user/maintainService/ServiceDetail.vue'
import PlaceOrder from '@/views/user/order/PlaceOrder.vue'
import OrderInfo from '@/views/user/order/OrderInfo.vue'
import Search from '@/views/user/search/SearchPage.vue'
import Fout from '@/views/404.vue'
import RecruitMaintainerVue from '@/views/user/recruit/RecruitMaintainer.vue'
// --------------维修员
import MaintainerLayout from '@/views/maintainer/MaintainerLayout.vue'
import MaintainerHome from '@/views/maintainer/MaintainerHome.vue'
import MaintainerInfo from '@/views/maintainer/MaintainerInfo.vue'
import OrderList from '@/views/maintainer/OrderList.vue'
import TakeOrder from '@/views/maintainer/TakeOrder.vue'
import AssignTakeOrder from '@/views/maintainer/AssignTakeOrder.vue'
import MChangePassword from '@/views/maintainer/ChangePassword.vue'
// -----------------管理员
import AdminLayout from '@/views/admin/AdminLayout.vue'
import AdiminHomeVue from '@/views/admin/AdiminHome.vue'
import AdminMaintainerVue from '@/views/admin/AdminMaintainer.vue'
import AdminUserVue from '@/views/admin/AdminUser.vue'
import AdminOrderVue from '@/views/admin/AdminOrder.vue'
import AdminLoginVue from '@/views/admin/AdminLogin.vue'
import AdminAdminerVue from '@/views/admin/AdminAdminer.vue'
import AdminCommentVue from '@/views/admin/AdminComment.vue'
Vue.use(VueRouter)

const routes = [
  // 用户
  {
    path: '/',
    redirect: '/main/home',
    component: LayoutContain, // 首页架子
    children: [
      { path: '/main/home', component: HomePage }, // 首页
      { path: '/main/maintainerlist', component: MaintainerList }, // 维修工列表
      { path: '/main/servicelist', component: ServiceList }, // 维修服务列表
      { path: '/user/placeorder', component: PlaceOrder }, // 填写订单信息
      { path: '/main/search/:search', component: Search }, // 搜索页面
      { path: '/main/recuit', component: RecruitMaintainerVue } // 招募工程师
    ]
  },
  {
    path: '/404',
    component: Fout // 404
  },
  {
    path: '/main/servicedetail/:id',
    component: ServiceDetail // 维修服务详细页
  },
  {
    path: '/login',
    component: UserLogin // 登录页
  },
  {
    path: '/reg',
    component: UserReg // 注册
  },
  {
    path: '/user/orderinfo',
    component: OrderInfo // 订单详情
  },
  // 用户个人中心
  {
    path: '/user',
    redirect: '/user/info',
    component: UserCenter, // 个人中心架子
    children: [{

      path: '/user/changepassword',
      component: ChangePassword // 修改密码
    },
    {
      path: '/user/info',
      component: UserInfo // 用户基本资料页
    },
    {
      path: '/user/order',
      component: UserOrder // 用户基本订单页
    },
    {
      path: '/user/address',
      component: UserAddress // 我的地址
    }
    ]
  },
  // 维修员
  {
    path: '/maintainer',
    redirect: '/maintainer/home',
    component: MaintainerLayout,
    children: [
      { path: '/maintainer/home', component: MaintainerHome }, // 首页
      { path: '/maintainer/info', component: MaintainerInfo }, // 个人信息
      { path: '/maintainer/orderlist', component: OrderList }, // 订单记录
      { path: '/maintainer/takeorder', component: TakeOrder }, // 接单
      { path: '/maintainer/assigntakeorder', component: AssignTakeOrder }, // 指定接单
      { path: '/maintainer/changepassword', component: MChangePassword } // 修改密码
    ]
  },
  // 管理员
  {
    path: '/admin',
    redirect: '/admin/home',
    component: AdminLayout,
    children: [
      { path: '/admin/home', component: AdiminHomeVue }, // 首页
      { path: '/admin/maintainer', component: AdminMaintainerVue }, // 管理维修员
      { path: '/admin/user', component: AdminUserVue }, // 管理用户
      { path: '/admin/order', component: AdminOrderVue }, // 管理订单
      { path: '/admin/adminer', component: AdminAdminerVue }, // 管理订单
      { path: '/admin/comment', component: AdminCommentVue }// 修改密码
    ]
  },
  { path: '/adlogin', component: AdminLoginVue } // 管理员登录
]

const router = new VueRouter({
  // 历史模式
  mode: 'history',
  routes
})

// 定义需要权限访问的正则表达式
const pattern = /^\/user\/|^\/maintainer\/|^\/admin\//
const maintainerPattern = /^\/maintainer/
const adminPattern = /^\/admin/
let token = ''
router.beforeEach(async (to, from, next) => {
  if (!pattern.test(to.path)) {
    return next()
  }
  if (maintainerPattern.test(to.path)) {
    token = store.getters.getMaintainerToken
  } else if (adminPattern.test(to.path)) {
    console.log(adminPattern.test(to.path))
    token = store.getters.getAdminToken
  } else {
    token = store.getters.getToken
  }
  if (token) {
    next()
  } else {
    await MessageBox.confirm('您还未登录！', '提示', {
      confirmButtonText: '去登录',
      cancelButtonText: '先不登录',
      type: 'warning'
    }).then(() => {
      if (adminPattern.test(to.path)) {
        next('/adlogin')
      } else {
        next('/login')
      }
    }).catch(() => {})
  }
})
export default router
